import {Injectable} from '@angular/core';
import {Platform} from 'ionic-angular';
import {HttpProvider} from '../../common/http'
import {API} from '../../common/api'

@Injectable()
export class LoginProvider {

  constructor(public http: HttpProvider,
              public platform: Platform) {

  }

  login(params) {
    params['platform'] = this.platform.platforms()[1]
    params['orgId'] = '888'
    return this.http.requestWithoutToken({
      type: 'POST',
      url: API.LOGIN,
      params: params
    }).then(res => {
      //登陆成功
      this.http.setTokenStorage(res)
    }).catch(err => {
      if (err.code == '70001') {
        //验证码已过期
        this.http.toastMsg('验证码已过期，请重新获取')
      } else if (err.code == '70002') {
        //验证码错误
        this.http.toastMsg('验证码错误，请重新输入')
      } else {
        this.http.toastMsg('系统错误，错误代码为：' + err.code)
      }
    })
  }

  getVerityCode() {
    return this.http.requestWithoutToken({
      type: 'POST',
      url: API.GET_SEND_VERITY_CODE,
      params: {
        'phone': '13958750139',
        'orgId': '888'
      }
    }).then(res => {

    })
  }
}
